package com.fanfanwang.ffw.app.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.AppContext;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.asynctask.ChkVersionAsyncTask;
import com.fanfanwang.ffw.app.base.BaseFragment;
import com.fanfanwang.ffw.app.bean.VersionBean;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.tools.UpdateManager;
import com.fanfanwang.ffw.app.ui.login.LoginAct;
import com.fanfanwang.ffw.app.ui.my.AccountAndSafetyAct;
import com.fanfanwang.ffw.app.ui.my.MsgSetAct;
import com.fanfanwang.ffw.app.ui.my.MyAboutAct;
import com.fanfanwang.ffw.app.ui.my.MyOffLineAct;
import com.fanfanwang.ffw.app.ui.my.MyUpdateInfoAct;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;

@SuppressLint("ValidFragment")
public class MainMyFragment extends BaseFragment {
	private LayoutInflater inflater;
	private CommonTitleBar commonTitleBar;
	private RelativeLayout rl_account_safety;
	private RelativeLayout rl_msg_set;
	private RelativeLayout rl_my_about;
	private RelativeLayout rl_my_exit;
	private RelativeLayout rl_my_info;
	private RelativeLayout rl_offline_map;
	private Context context;
	private int i = 0;// 0表示第一个显示，1表示第二个显示，2表示第三个fragement显示。
	private NetworkImageView iv_head;
	private RelativeLayout rl_version;
	public static IChkVersion iChkVersion;
	private TextView my_account;
	private TextView version_name;
	private VersionBean versionBean;
	/* 更新进度条 */
	private ProgressBar mProgress;
	private PackageInfo pi;
	private String versionName;
	@SuppressLint("ValidFragment")
	public MainMyFragment(Context context) {
		// TODO Auto-generated constructor stub
		super();
		inflater = LayoutInflater.from(context);
		this.context = context;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View settingLayout = inflater.inflate(R.layout.fragment_my_layout,
				container, false);
		return settingLayout;
	}

	public static MainMyFragment getMyFragment(Context context) {

		MainMyFragment mainMyFragment = new MainMyFragment(context);
		return mainMyFragment;
	}

	@SuppressLint("NewApi")
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		getActivity().getWindow().setFormat(PixelFormat.TRANSLUCENT);
//		initView(view);
//		initData();
//		setListener();
	}

	@Override
	public void initView(View view) {
		commonTitleBar = (CommonTitleBar) view
				.findViewById(R.id.common_titlebar);
		rl_msg_set = (RelativeLayout) view.findViewById(R.id.rl_msg_set);
		rl_account_safety = (RelativeLayout) view
				.findViewById(R.id.rl_account_safety);
		rl_my_about = (RelativeLayout) view.findViewById(R.id.rl_my_about);
		rl_my_exit = (RelativeLayout) view.findViewById(R.id.rl_my_exit);
		rl_my_info = (RelativeLayout) view.findViewById(R.id.rl_my_info);
		rl_offline_map = (RelativeLayout) view
				.findViewById(R.id.rl_offline_map);
		iv_head = (NetworkImageView) view.findViewById(R.id.iv_head);
		rl_version = (RelativeLayout) view.findViewById(R.id.rl_version);
		my_account = (TextView) view.findViewById(R.id.my_account);
		version_name = (TextView) view.findViewById(R.id.version_name);
	}

	@Override
	public void initData() {
		commonTitleBar.setCenterText(getString(R.string.main_tab_my));
		iv_head.setBackgroundResource(R.drawable.default_iv_head);
		iv_head.setDefaultImageResId(R.drawable.default_iv_head);
		iv_head.setErrorImageResId(R.drawable.default_iv_head);
		iv_head.setImageUrl("", AppContext.imageLoader);
		if (!TextUtils.isEmpty(SharedPreferencesUtil.getInstance(getActivity()).getUsername())) {
			my_account.setText(SharedPreferencesUtil.getInstance(getActivity()).getUsername());
		}
		PackageManager pm = getActivity().getPackageManager(); 
		try {
			 pi = pm.getPackageInfo(getActivity().getPackageName(), 0);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 versionName=pi.versionName;
		 version_name.setText(getActivity().getResources().getString(R.string.toast_now_version)+versionName);
		//getPackageName()是你当前类的包名，0代表是获取版本信息String name = pi.versionName;int code = pi.versionCode;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	/**
	 * 提示对话框
	 * 
	 * @param message
	 */
	public void showLogoutDialog(String message) {
		new AlertDialog.Builder(context)
				.setTitle(getString(R.string.exit_title))
				.setMessage(message)
				.setPositiveButton(getString(R.string.confirm),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								// sharedPreferencesUtil.putIsLogin(false);
								/*
								 * sharedPreferencesUtil.putUserId(null);
								 * sharedPreferencesUtil.putMobile(null);
								 * sharedPreferencesUtil.putUserNickName(null);
								 */
								// SharedPreferencesUtil.getInstance(PersonalSetAct.this).putUserName(null);
								// SharedPreferencesUtil.getInstance(PersonalSetAct.this).putUserId(null);
								// //
								// SharedPreferencesUtil.getInstance(PersonalSetAct.this).putHeadUrl(null);
								// AppConstant.user_id = "";
								// // AppConstant.username = "";
								// AppConstant.user_nickName = "";
								// // AppConstant.headimg = "";
								SharedPreferencesUtil
										.getInstance(getActivity()).putUserId(
												null);
								SharedPreferencesUtil
										.getInstance(getActivity())
										.putUsername(null);
								SharedPreferencesUtil
										.getInstance(getActivity())
										.putTruename(null);
								SharedPreferencesUtil
										.getInstance(getActivity())
										.putCarnumber(null);
								AppManager.getAppManager().finishAllActivity();
								AppConstant.user_id = "";
								AppConstant.username = "";
								Intent mIntent = new Intent(getActivity(),
										LoginAct.class);
								mIntent.putExtra("isNeedIntent", true);
								startActivity(mIntent);
								getActivity().finish();
							}
						})
				.setNegativeButton(getString(R.string.cancel),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						}).show();
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub
		rl_offline_map.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), MyOffLineAct.class);
				startActivity(intent);
			}
		});
		rl_my_info.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), MyUpdateInfoAct.class);
				startActivity(intent);
			}
		});
		rl_my_about.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), MyAboutAct.class);
				startActivity(intent);
			}
		});
		rl_account_safety.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						AccountAndSafetyAct.class);
				startActivity(intent);
			}
		});
		rl_msg_set.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), MsgSetAct.class);
				startActivity(intent);
			}
		});
		rl_my_exit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showLogoutDialog(getString(R.string.exit_info));
			}
		});
		rl_version.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setVersionTask();
			}
		});

	}

	/**
	 * 版本更新的
	 */
	private void setVersionTask() {
		new ChkVersionAsyncTask(getActivity()).execute("");
		iChkVersion = new IChkVersion() {

			@Override
			public void setVersion(VersionBean versionBean, Activity mAct) {
				// TODO Auto-generated method stub
				final UpdateManager updateManager = new UpdateManager(mAct);
				String version = updateManager.getVersionCode(mAct);
				if (!version.equals(versionBean.version)) {
					updateManager.showNoticeDialog(versionBean);
				} else {
					ToastAlone.showToast(mAct, "当前版本已经是最新版本！");
				}
			}
		};
	}

	public interface IChkVersion {
		void setVersion(VersionBean versionBean, Activity mAct);
	}

}
